﻿package mortal.game.view.common.display
{
    import com.gengine.global.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.controls.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.manager.*;
    import mortal.game.view.common.*;

    public class TextInputList extends GSprite
    {
        public var _textInput:GTextInput;
        public var _tileList:GTileList;
        public var _bg:ScaleBitmap;
        public var _listContainer:GSprite;
        public var defultString:String;
        public var textChangeCallBack:Function;
        public var selectdata:Object;

        public function TextInputList()
        {
            this.defultString = Language.getString(20714);
            return;
        }// end function

        private function onAddToStageHandler(event:Event) : void
        {
            removeEventListener(Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            Global.stage.addEventListener(MouseEvent.CLICK, this.onMouseClickHandler, true);
            addEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }// end function

        private function onRemoveFromStageHandler(event:Event) : void
        {
            Global.stage.removeEventListener(MouseEvent.CLICK, this.onMouseClickHandler, true);
            removeEventListener(Event.REMOVED_FROM_STAGE, this.onRemoveFromStageHandler);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.align = TextFormatAlign.LEFT;
            this._textInput = UIFactory.gTextInput(0, 0, 140, 22, this);
            this._textInput.setStyle("textFormat", _loc_1);
            this._textInput.text = "";
            this._listContainer = UIFactory.getUICompoment(GSprite, 0, 22, this);
            var _loc_2:Boolean = true;
            this._listContainer.mouseEnabled = true;
            this._listContainer.mouseChildren = _loc_2;
            this._listContainer.width = 140;
            this._bg = UIFactory.bg(0, 0, 140, 140, this._listContainer);
            this._tileList = UIFactory.tileList(0, 0, 140, 140, this._listContainer);
            this._tileList.columnWidth = 140;
            this._tileList.rowHeight = 20;
            this._tileList.direction = ScrollBarDirection.VERTICAL;
            this._tileList.horizontalGap = 0;
            this._tileList.verticalGap = 0;
            this._tileList.styleName = "TileList";
            this._tileList.setStyle("skin", new Bitmap());
            this._tileList.setStyle("cellRenderer", TextInputListRenderer);
            this._tileList.configEventListener(MouseEvent.CLICK, this.lisetSecletHandler);
            this._textInput.configEventListener(Event.CHANGE, this.onTextInputChangeHandler);
            this._textInput.configEventListener(FocusEvent.FOCUS_IN, this.onTextInputFocusInHandler);
            this._textInput.configEventListener(FocusEvent.FOCUS_OUT, this.onTextInputFocusOutHandler);
            this.onTextInputFocusOutHandler(null);
            this.onMouseClickHandler(null);
            addEventListener(Event.ADDED_TO_STAGE, this.onAddToStageHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this.textChangeCallBack = null;
            this._tileList.removeEventListener(MouseEvent.CLICK, this.lisetSecletHandler);
            this._textInput.removeEventListener(Event.CHANGE, this.onTextInputChangeHandler);
            this._textInput.removeEventListener(FocusEvent.FOCUS_IN, this.onTextInputFocusInHandler);
            this._textInput.removeEventListener(FocusEvent.FOCUS_OUT, this.onTextInputFocusOutHandler);
            this._textInput.dispose(param1);
            this._tileList.dispose(param1);
            this._bg.dispose(param1);
            this._listContainer.dispose(param1);
            this._textInput = null;
            this._tileList = null;
            this._bg = null;
            this._listContainer = null;
            this.selectdata = null;
            super.disposeImpl(param1);
            return;
        }// end function

        public function init(param1:Function) : void
        {
            this.textChangeCallBack = param1;
            return;
        }// end function

        private function lisetSecletHandler(event:MouseEvent = null) : void
        {
            if (this._tileList.selectedItem)
            {
                this._textInput.text = this._tileList.selectedItem.name;
                this.selectdata = this._tileList.selectedItem;
                dispatchEvent(new Event(Event.SELECT));
            }
            this.onMouseClickHandler();
            return;
        }// end function

        protected function onTextInputChangeHandler(event:Event) : void
        {
            this.selectdata = null;
            if (this.textChangeCallBack != null)
            {
                this.textChangeCallBack();
            }
            return;
        }// end function

        protected function onTextInputFocusInHandler(event:FocusEvent) : void
        {
            if (this._textInput == null)
            {
                return;
            }
            if (this._textInput.text != "" && this._textInput.text == this.defultString)
            {
                this._textInput.text = "";
            }
            return;
        }// end function

        protected function onTextInputFocusOutHandler(event:FocusEvent) : void
        {
            if (this._textInput.text == "")
            {
                this._textInput.htmlText = "<font color=\'#a49e91\'>" + this.defultString + "</font>";
            }
            return;
        }// end function

        public function updateDataProvider(param1:DataProvider) : void
        {
            this._tileList.dataProvider = param1;
            if (param1.length < 7)
            {
                this._bg.height = param1.length * 20;
            }
            else
            {
                this._bg.height = 140;
            }
            if (param1.length > 0)
            {
                this._tileList.visible = true;
            }
            LayerManager.topLayer.addChild(this._listContainer);
            var _loc_2:* = this.localToGlobal(new Point(0, 0));
            this._listContainer.move(_loc_2.x, _loc_2.y + 22);
            return;
        }// end function

        public function updateDataProvider1(param1:Array) : void
        {
            this.updateDataProvider(new DataProvider(param1));
            return;
        }// end function

        protected function onMouseClickHandler(event:MouseEvent = null) : void
        {
            if (this._listContainer.parent)
            {
                this._listContainer.parent.removeChild(this._listContainer);
            }
            return;
        }// end function

    }
}
